﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using KerriganDataContract.Agency;
using KerriganDataContract.Person;
using System.Diagnostics;

namespace KerriganService.DAC
{
    public class AgencyEmployeeDAC
    {


        public static AgencyEmployeeDC checkLogin(string login, string password) {
            AgencyEmployeeDC result = null;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                   result = CopyEntityToBo( db.TAgencyEmployees.FirstOrDefault(f => f.TPerson.NickName == login && f.TPerson.Password == password));
                }
                catch (Exception ex)
                {
                    result = null;
                    Debug.WriteLine(ex.Message);
                }
            }
            return result;
        }

        public static TAgencyEmployee CopyBoToEntity(AgencyEmployeeDC bo)
        {
            TAgencyEmployee entity = new TAgencyEmployee();

            try
            {
                entity.IdAgencyEmployee = bo.IdAgencyEmployee;
                entity.TPerson = new TPerson();         
                entity.TPerson = PersonDAC.CopyBoToEntity(bo.Person);               
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }


        public static AgencyEmployeeDC CopyEntityToBo(TAgencyEmployee entity)
        {
            AgencyEmployeeDC bo = new AgencyEmployeeDC();

            try
            {
                bo.IdAgencyEmployee = entity.IdAgencyEmployee;
                bo.Person = new PersonDC();
                bo.Person = PersonDAC.CopyEntityToBo(entity.TPerson);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }
    }
}